(function (window, undefined ){
var AWE = {
		openTab : function(id,title,href) {
			//判断该tab页是否已存在
			var allTabs=$('.easyui-tabs').tabs('tabs');
			var index=0;
			var size=allTabs.length;
			if(size>0){
				for(;index<allTabs.length;index++){
					if(allTabs[index].panel('options').id==id){
						break;
					}
				}
			}
			if($('.easyui-tabs').tabs('exists',index)){
				$('.easyui-tabs').tabs('select',index);
			}else{
				$('.easyui-tabs').tabs('add', {
					title : title,
					id:id,
					content : '<iframe scrolling="yes" frameborder="0" name="'+id+'"  src="'+href+'" style="width:100%;height:100%;"></iframe>',
					//iconCls : 'icon-save',
					closable : true,
					border:false,
					fit:true
				});
			}
		},
		searchDialog:function(dw,json){
			if($('.searchDialog').attr('flag')!=1){//检查查询面板标志值是否存在
				var sb=new StringBuffer();
				sb.append("<div id='search_form' title='查询' style='width:600px;height:400px;'>");
				sb.append("<table id='pg' class='easyui-propertygrid'");
				for(var i=0;i<json.length;i++){
					var column=$(dw).datagrid('getColumnOption',json[i]["field"]);
					var fieldTitle=column['title'];
					var type=json[i]['type'];
					//if("input[text]"!=type)continue;
					sb.append("<tr>");
					sb.append("<td>").append(fieldTitle).append(":</td>");
					//普通单行文本框
					if("input[text]"==type){
						sb.append("<td><input column='"+json[i]["column"]+"' field='"+json[i]["field"]+"' class='search' type='text' id='search_form"+json[i]["field"]+"'/></td>");
					}else if('input[checkbox]'==type&&json[i]['codeLib']){
						sb.append("<td><div  class='search checkbox' column='"+json[i]["column"]+"' field='"+json[i]["field"]+"'>");
						for(var j=0;j<json[i]['codeLib'].length;j++){
							sb.append("<input name='"+json[i]["field"]+"' type='checkbox' id='search_form"+json[i]["field"]+json[i]['codeLib'][j]['itemNo']+"' value='"+json[i]['codeLib'][j]['itemNo']+"'/>");
							sb.append("<label for='search_form"+json[i]["field"]+json[i]['codeLib'][j]['itemNo']+"'>"+json[i]['codeLib'][j]['itemName']+"</label>");
						}
						sb.append("</div></td>");
					}
					//下拉单选框
					else if('select'==type&&json[i]['codeLib']){
						sb.append("<td><select field='"+json[i]["field"]+"' column='"+json[i]["column"]+"' class='search'  id='search_form"+json[i]["field"]+"'>");
						sb.append("<option></option>");
						for(var j=0;j<json[i]['codeLib'].length;j++){
							sb.append("<option value='").append(json[i]['codeLib'][j]['itemNo']).append("'>").append(json[i]['codeLib'][j]['itemName']).append("</option>");
						}
						sb.append("</select></td>");
					}
					//时间组件
					else if("input[datetime]"==type){
						sb.append("<td><input field='"+json[i]["field"]+"' column='"+json[i]["column"]+"' type='text' class='search easyui-datetimebox start'/> -- <input type='text' field='"+json[i]["field"]+"' class='search easyui-datetimebox end'/></td>");
					}
					sb.append("</tr>");
				}
				sb.append("</table></div>");
				
				$('.searchDialog').attr('flag',1).html(sb.toString());
				$('#search_form .easyui-datetimebox').css({width:"135px"}).datetimebox();
				$('.easyui-datetimebox ').datetimebox();
			}
			$('#search_form').dialog({
				iconCls:'icon-search',
				resizable:true,
				modal:true,
				buttons:[{
					text:'清空',
					iconCls:'icon-edit',
					handler:function(){
						$('.search').val("");
						var queryParam=$(dw).datagrid('options')['queryParams'];
						queryParam['searchFilter']="";
						$(dw).datagrid('load',queryParam);
						$('#search_form').dialog('close');
					}
				},{
					text:'查询',
					iconCls:'icon-search',
					handler:function(){
						var searchFilter="";
						var searchFields=$(".search");
						for(var i=0;i<searchFields.length;i++){
							var value="";
							var type="";
							if($(searchFields[i]).hasClass("easyui-datetimebox")){
								value=$(searchFields[i]).datetimebox('getValue');
								type='input[datetime]';
							}else if($(searchFields[i]).hasClass("checkbox")){
								type='input[checkbox]';
								value=new Array();
								$('input[name='+$(searchFields[i]).attr('field')+"]:checked").each(function(){
									value.push("'"+$(this).val().replace("'","''")+"'");
							     });
							}else{
								if(searchFields[i].tagName.toLowerCase()=='input'){
									if(searchFields[i].type=='text'){
										type='input[text]';
									}
								}else if(searchFields[i].tagName.toLowerCase()=='select'){
									type='select';
								}
								value=$(searchFields[i]).val();
							}
							if(value.length>0){
								if(searchFilter!=""){
									searchFilter+=" and ";
								}
								
								if(type=='input[text]'){
									searchFilter+=$(searchFields[i]).attr('column')+" like '%"+value.replace("'","''")+"%' ";
								}else if(type=='input[datetime]'){
									searchFilter+=$(searchFields[i]).attr('column')+($(searchFields[i]).hasClass("start")?">=":"<=")+" '"+value.replace("'","''")+"' ";
								}else if(type=='input[checkbox]'){
									if(value.length>0){
										searchFilter+=$(searchFields[i]).attr('column')+" in ("+value.join(",")+") ";
									}
								}else if(type=='select'){
									searchFilter+=$(searchFields[i]).attr('column')+" = '"+value.replace("'","''")+"' ";
								}
							}
						}
						var queryParam=$(dw).datagrid('options')['queryParams'];
						queryParam['searchFilter']=searchFilter;
						$(dw).datagrid('load',queryParam);
						$('#search_form').dialog('close');
					}
				}],
				onOpen:function(){
			    	$('.window-mask').dblclick(function(){
			    		if($(this).prevUntil('.window').length==1){
			    			$('#search_form').dialog('close');
			    		}
			    	});
			    },
			});
		},
		OpenLayout:function(obj,position,sURL,sPara){
			if (undefined != sPara) {
				if(sURL.indexOf("?") >= 0){
					sURL = sWebRoot + sURL + "&"
					+ sPara;
					
				}else{
					sURL = sWebRoot + sURL + "?"
						+ sPara;
				}
			} else {
				sURL = sWebRoot + sURL;
			}
			$(obj).layout('panel',position).parent().children("*").removeClass("panel-noscroll");
			$(obj).layout('panel',position).panel('refresh',sURL);
		},
		OpenDwLayout:function(obj,position,page,param){
			//OpenLayout(obj, position, "comm/datawindow_page.jsp", "pagePath="+encodeURI(page)+"&"+param);
			AWE.OpenLayout(obj, position, "datawindow_page.html", "pagePath="+encodeURI(page)+"&"+param);
		},
		Prompt:function(msg,fn) {
			var result = false;
			$.messager.prompt('确认', msg, function(r) {
				result = r;
				fn(r);
			});
		},
};
/*
AWE.Alert = function(msg) {
	$.messager.alert('消息', msg);
};

AWE.Error = function(msg) {
	$.messager.alert('错误', msg, 'error');
};

AWE.Info = function(msg) {
	$.messager.alert('提示', msg, 'info');
};

AWE.Warn = function(msg) {
	$.messager.alert('警告', msg, 'warning');
};

AWE.Confirm = function(msg) {
	var result = false;
	$.messager.confirm('确认', msg, function(r) {
		result = r;
	});
	return result;
};

AWE.Prompt = function(msg,fn) {
	var result = false;
	$.messager.prompt('确认', msg, function(r) {
		result = r;
		fn(r);
	});
};*/



window.AWE=AWE;
})(window);